package com.example.shop_2302.fg

import androidx.lifecycle.viewModelScope
import com.example.base.entity.GoodsEntity
import com.example.base.entity.IndexEntity
import com.example.base.entity.QuanEntity
import com.example.base.entity.Res
import com.example.base.mvvm.BaseViewModel
import com.example.shop_2302.utils.SingleLiveData
import kotlinx.coroutines.launch

class HomeViewModel:BaseViewModel() {
    //返回值 --首页数据
    val indexSuccess = SingleLiveData<Res<IndexEntity>>()
    val indexFail = SingleLiveData<String>()

    fun index(){
        viewModelScope.launch {
            try {
                indexSuccess.postValue(apiServer.index())
            }catch (e:Exception){
                indexFail.postValue(e.message.toString())
            }
        }
    }
    //返回值--商品数据
    val goodsSuccess = SingleLiveData<Res<GoodsEntity>>()
    val goodsFail = SingleLiveData<String>()
    fun indexGoods(type:Int,limit:Int,page:Int){
        viewModelScope.launch {
            try {
                goodsSuccess.postValue(apiServer.indexGoods(type, limit, page))
            }catch (e:Exception){
                goodsFail.postValue(e.message.toString())
            }
        }
    }
    //优惠券列表
    val quanSuccess = SingleLiveData<Res<QuanEntity>>()
    val quanFail = SingleLiveData<String>()
    fun quan(limit:String,page:String,type:String){
        viewModelScope.launch {
            try {
                quanSuccess.postValue(apiServer.quan(limit,page,type))
            }catch (e:Exception){
                quanFail.postValue(e.message.toString())
            }
        }
    }
    //领取优惠券
    val recevieSuccess = SingleLiveData<Res<String?>>()
    val receiveFail = SingleLiveData<String>()
    fun quanReceive(couponId:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["couponId"] = couponId
                recevieSuccess.postValue(apiServer.quanReceive(mapToBody(map)))
            }catch (e:Exception){
                receiveFail.postValue(e.message.toString())
            }
        }
    }

}